package com.shop.Control;

import java.util.Map;

import com.JSON2.json.JSONObject;
import com.JSON2.json.JSONTokener;
import com.opensymphony.xwork2.ActionContext;
import com.shop.service.MemberServiceImpl;
import com.shop.service.SettlementServiceImpl;
import com.shop.util.BeanUtil;
import com.shop.util.FormAjaxUtil;
import com.shop.util.ParamMapUtil;

/**
 * 
 * <b>会员控制类<b>
 * <pre>
 * submitOrders：
 * 	订单提交
 * initSubmitOrdersPage:
 * 	初始化下单页
 * </pre>
 * @author 威 
 * <br>2018年3月13日 下午5:07:34
 *
 */
public class MemberAction extends SuperActionSupport {
	private static final long serialVersionUID = 7539369474585568995L;
	
	public MemberServiceImpl getService(){
		return (MemberServiceImpl) BeanUtil.getBean("memberServiceImpl");
	}
	//初始化订单
	public String initOrder(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		/*
		ActionContext.getContext().getSession().get("member_id");
		假设此时的id为1
		*/
		System.out.println("刷新订单信息");
		JSONObject object = new JSONObject();
		object.put("orders", getService().getOrder((Integer) session.get("member_id")));
		object.put("index", session.get("linkToPageIndex"));
		print(object.toString());
		return SUCCESS;
	}
	//初始化收藏
	public String initColle(){
		/*
		ActionContext.getContext().getSession().get("member_id");
		假设此时的id为1
		*/
		print(getService().getColle(1).toString());
		return SUCCESS;
	}
	//初始化购物车
	public String initCar(){
		print(getService().getCar(1).toString());
		return SUCCESS;
	}
	//初始化足迹
	public String initFoot(){
		print(getService().getFoot(1).toString());
		return SUCCESS;
	}
	//初始化信息
	public String initInfor(){
		print(getService().getInfor(1).toString());
		return SUCCESS;
	}
	//浏览商品时对的请求
	public String requestGoodsPage(){
		print("get");
		return SUCCESS;
	}
	//添加购物车
	public String addCar(){
		Map parameterMap = ActionContext.getContext().getParameters();
		JSONObject obj = (JSONObject) JSONTokener.parseObject(((String[]) parameterMap.get("data"))[0]);
		String dataStr = getService().addCar((Integer) ActionContext.getContext().getSession().get("member_id"), obj);
		print(dataStr); //存在返回true 不存在返回id
		return SUCCESS;
	}
	//添加收藏
	public String addColle(){
		Map parameterMap = ActionContext.getContext().getParameters();
		Integer id = Integer.parseInt(((String[]) parameterMap.get("data"))[0]);
		String dataStr = getService().addColle((Integer) ActionContext.getContext().getSession().get("member_id"), id);
		print(dataStr);
		return SUCCESS;
	}
	public String addFoot(){
		
		return SUCCESS;
	}
	//跳转至单个商品
	public String linkToGoods(){
		Map parameterMap = ActionContext.getContext().getParameters();
		ActionContext.getContext().getSession().put("goods_id", Integer.parseInt(((String[]) parameterMap.get("data"))[0]));
		print("true");
		return SUCCESS;
	}
	//购物车点击进行结算，上传结算数据
	public String settlement (){
		System.out.println("结算");
		Map map = ActionContext.getContext().getParameters();
		String data = ((String[]) map.get("data"))[0];
		System.out.println(data);
		ActionContext.getContext().getSession().put("data", data);
		print("true");
		return SUCCESS;
	}
	/**
	 * 负责注册响应
	 * <p>	 
	 * @return
	 * String
	 * @see
	 * @since 1.0
	 */
	public String register(){
		System.out.println("register");
		ActionContext context = ActionContext.getContext();
		ParamMapUtil paramMapUtil = new ParamMapUtil(context.getParameters());
		JSONObject obj = paramMapUtil.getJSONData("username", "password", 
				"trueName", "sex", "phoneNumber", "email");
		if(getService().register(obj))
			print(FormAjaxUtil.successAccess(
					(String) paramMapUtil.get("callbackObj"), "true"));
		else 
			print(FormAjaxUtil.failAccess(
					(String) paramMapUtil.get("callbackObj"), "false"));
		return SUCCESS;
	}
	public String getPrivili(){
		Integer id = (Integer) ActionContext.getContext().getSession().get("member_id");
		if(id != null) print("true");
		else print("false");
		return SUCCESS;
	}
	//会员登录
	@SuppressWarnings("rawtypes")
	public String login(){
		ActionContext context=ActionContext.getContext();
		Map parameterMap=context.getParameters();
		String username = ((String[]) parameterMap.get("user"))[0];
		String password = ((String[]) parameterMap.get("password"))[0];
		String callbackObj = ((String[]) parameterMap.get("callbackObj"))[0];
		try{
			Object[] result = getService().login(username, password);
			switch((String) result[0]){
				case "0000":
					context.getSession().put("member_id", result[1]);
					print(FormAjaxUtil.successAccess(callbackObj, "true"));
					break;
				case "0001":
					print(FormAjaxUtil.failAccess(callbackObj, "用户不存在"));
					break;
				case "0002":
					print(FormAjaxUtil.failAccess(callbackObj, "密码有误"));
					break;
			}
		}catch (RuntimeException e){
			e.printStackTrace();
			System.out.println("异常失败");
			print(FormAjaxUtil.failAccess(callbackObj, "服务器异常"));
		}
		return SUCCESS;
	}
	//在结算页面点击结算按钮进行提交订单
	public String submitOrders(){
		System.out.println("submitOrders-0");
		String data = ((String[]) ActionContext.getContext().getParameters().get("data"))[0];
		System.out.println("submitOrders-1");
		((SettlementServiceImpl) BeanUtil.getBean("settlementServiceImpl"))
		.submitOrders(data);
		System.out.println("submitOrders-end");
		print("ok");
		return SUCCESS;
	}
	
	//初始化结算页面
	public String initSubmitOrdersPage(){
		print(((SettlementServiceImpl) BeanUtil.getBean("settlementServiceImpl"))
				.initSettPage());
		return SUCCESS;
	}
	public String linkTo(){
		/*Map<String, Object> session = ActionContext.getContext().getSession();
		Integer id = (Integer) session.get("member_id");
		session.put("linkToPageIndex", ((String []) ActionContext.getContext()
				.getParameters().get("type"))[0]);*/
		//print(id != null ? "true" : "false");
		System.out.println("请求linkTO");
		print("true");
		return SUCCESS;
	}
	public String test(){
		print("true");
		return SUCCESS;
	}
}
